### Preliminary ----------------------------------------------------------------
# Load libraries
library(MatchIt)
library(dplyr)
library(tidyverse)
library(stringr)
library(lfe)
library(stargazer)

# Focus on just the data where the necessary matching and outcome variables are available
full_data <- read.csv("Matching Input.csv")
trimmed_data <- full_data[complete.cases(full_data %>% select(-leader, -comm_chair, -prior_office)),]
ran_data <- read.csv("Congressional Candidacies.csv")

# Subset the data based on majority status
min_data <- trimmed_data %>% filter(majority_member == 0)
maj_data <- trimmed_data %>% filter(majority_member == 1)

### Table 2 --------------------------------------------------------------------
# Run matches for main analyses
matches_full <- matchit(viable ~ party_pres_vs + seniority + factor(imputed_gender) +
                         factor(state) + factor(chamber) + factor(party) + factor(election_year),
                       estimand = "ATT",
                       method = "cem",
                       data = trimmed_data)
matches_maj <- matchit(viable ~ party_pres_vs + seniority + factor(imputed_gender) +
                         factor(state) + factor(chamber) + factor(party) + factor(election_year),
                       estimand = "ATT",
                       method = "cem",
                       data = maj_data)
matches_min <- matchit(viable ~ party_pres_vs + seniority + factor(imputed_gender) +
                         factor(state) + factor(chamber) + factor(party) + factor(election_year),
                       estimand = "ATT",
                       method = "cem",
                       data = min_data)

# Run analyses for Table 2
full.fit <- felm(avg_opp_prop ~ viable + party_pres_vs + seniority + factor(imputed_gender) |
                   interaction(state, chamber, party, election_year) | 0 | klarner_id,
                 data = trimmed_data, weights = matches_full$weights)
maj.fit <- felm(avg_opp_prop ~ viable + party_pres_vs + seniority + factor(imputed_gender) |
                  interaction(state, chamber, party, election_year) | 0 | klarner_id,
                data = maj_data, weights = matches_maj$weights)
min.fit <- felm(avg_opp_prop ~ viable + party_pres_vs + seniority + factor(imputed_gender) |
                  interaction(state, chamber, party, election_year) | 0 | klarner_id,
                data = min_data, weights = matches_min$weights)

full.np.fit <- felm(extremity ~ viable + party_pres_vs + seniority + factor(imputed_gender) |
                      interaction(state, chamber, party, election_year) | 0 | klarner_id,
                    data = trimmed_data, weights = matches_full$weights)
maj.np.fit <- felm(extremity ~ viable + party_pres_vs + seniority + factor(imputed_gender) |
                     interaction(state, chamber, party, election_year) | 0 | klarner_id,
                   data = maj_data, weights = matches_maj$weights)
min.np.fit <- felm(extremity ~ viable + party_pres_vs + seniority + factor(imputed_gender) |
                     interaction(state, chamber, party, election_year) | 0 | klarner_id,
                   data = min_data, weights = matches_min$weights)

# Table 2
stargazer(full.fit, maj.fit, min.fit, full.np.fit, maj.np.fit, min.np.fit)
c(sum(matches_full$weights > 0), sum(matches_maj$weights > 0), sum(matches_min$weights > 0))

# Get substantive significance of Table 2 coefficients
magnitude.fit <- felm(avg_opp_prop ~ viable + party_pres_vs | interaction(state, chamber, party, election_year), data = trimmed_data)
magnitude.np.fit <- felm(extremity ~ viable + party_pres_vs | interaction(state, chamber, party, election_year), data = trimmed_data)

coef(full.fit)[1]/coef(magnitude.fit)[2]
sd(trimmed_data$party_pres_vs)
coef(full.np.fit)[1]/coef(magnitude.np.fit)[2]

### Table C.1 ------------------------------------------------------------------
# Run matching analyses for Table C.1
full.swing.fit <- felm(avg_opp_prop ~ viable + viable_swing + party_pres_vs + seniority + factor(imputed_gender) |
                         interaction(state, chamber, party, election_year) | 0 | klarner_id,
                       data = trimmed_data, weights = matches_full$weights)
maj.swing.fit <- felm(avg_opp_prop ~ viable + viable_swing + party_pres_vs + seniority + factor(imputed_gender) |
                        interaction(state, chamber, party, election_year) | 0 | klarner_id,
                      data = maj_data, weights = matches_maj$weights)
min.swing.fit <- felm(avg_opp_prop ~ viable + viable_swing + party_pres_vs + seniority + factor(imputed_gender) |
                        interaction(state, chamber, party, election_year) | 0 | klarner_id,
                      data = min_data, weights = matches_min$weights)

full.swing.np.fit <- felm(extremity ~ viable + viable_swing + party_pres_vs + seniority + factor(imputed_gender) |
                            interaction(state, chamber, party, election_year) | 0 | klarner_id,
                          data = trimmed_data, weights = matches_full$weights)
maj.swing.np.fit <- felm(extremity ~ viable + viable_swing + party_pres_vs + seniority + factor(imputed_gender)|
                           interaction(state, chamber, party, election_year) | 0 | klarner_id,
                         data = maj_data, weights = matches_maj$weights)
min.swing.np.fit <- felm(extremity ~ viable + viable_swing + party_pres_vs + seniority + factor(imputed_gender) |
                           interaction(state, chamber, party, election_year) | 0 | klarner_id,
                         data = min_data, weights = matches_min$weights)

# Table C.1
stargazer(full.swing.fit, maj.swing.fit, min.swing.fit)
stargazer(full.swing.np.fit, maj.swing.np.fit, min.swing.np.fit)

# Run regression analyses for Table C.2
reg.full.swing.fit <- felm(avg_opp_prop ~ viable + viable_swing + party_pres_vs + seniority + factor(imputed_gender) |
                             interaction(state, chamber, party, election_year) | 0 | klarner_id,
                           data = full_data)
reg.maj.swing.fit <- felm(avg_opp_prop ~ viable + viable_swing + party_pres_vs + seniority + factor(imputed_gender)|
                            interaction(state, chamber, party, election_year) | 0 | klarner_id,
                          data = maj_data)
reg.min.swing.fit <- felm(avg_opp_prop ~ viable + viable_swing + party_pres_vs + seniority + factor(imputed_gender)|
                            interaction(state, chamber, party, election_year) | 0 | klarner_id,
                          data = min_data)

reg.full.swing.np.fit <- felm(extremity ~ viable + viable_swing + party_pres_vs + seniority + factor(imputed_gender)|
                                interaction(state, chamber, party, election_year) | 0 | klarner_id,
                              data = full_data)
reg.maj.swing.np.fit <- felm(extremity ~ viable + viable_swing + party_pres_vs + seniority + factor(imputed_gender)|
                               interaction(state, chamber, party, election_year) | 0 | klarner_id,
                             data = maj_data)
reg.min.swing.np.fit <- felm(extremity ~ viable + viable_swing + party_pres_vs + seniority + factor(imputed_gender)|
                               interaction(state, chamber, party, election_year) | 0 | klarner_id,
                             data = min_data)

# Table C.2
stargazer(reg.full.swing.fit, reg.maj.swing.fit, reg.min.swing.fit)
stargazer(reg.full.swing.np.fit, reg.maj.swing.np.fit, reg.min.swing.np.fit)

### Table E.1 ------------------------------------------------------------------
# Run regressions for Table E.1
reg.full.fit <- felm(avg_opp_prop ~ viable + party_pres_vs + seniority + factor(imputed_gender) |
                       interaction(state, chamber, party, election_year) | 0 | klarner_id,
                     data = trimmed_data)
reg.maj.fit <- felm(avg_opp_prop ~ viable + party_pres_vs + seniority + factor(imputed_gender)|
                      interaction(state, chamber, party, election_year) | 0 | klarner_id,
                    data = maj_data)
reg.min.fit <- felm(avg_opp_prop ~ viable + party_pres_vs + seniority + factor(imputed_gender)|
                      interaction(state, chamber, party, election_year) | 0 | klarner_id,
                    data = min_data)

reg.full.np.fit <- felm(extremity ~ viable + party_pres_vs + seniority + factor(imputed_gender)|
                          interaction(state, chamber, party, election_year) | 0 | klarner_id,
                        data = trimmed_data)
reg.maj.np.fit <- felm(extremity ~ viable + party_pres_vs + seniority + factor(imputed_gender)|
                         interaction(state, chamber, party, election_year) | 0 | klarner_id,
                       data = maj_data)
reg.min.np.fit <- felm(extremity ~ viable + party_pres_vs + seniority + factor(imputed_gender)|
                         interaction(state, chamber, party, election_year) | 0 | klarner_id,
                       data = min_data)

# Table E.1
stargazer(reg.full.fit, reg.maj.fit, reg.min.fit)
stargazer(reg.full.np.fit, reg.maj.np.fit, reg.min.np.fit)

# Run matches for analyses with different overlap thresholds
matches_full_25 <- matchit(viable_25 ~ party_pres_vs + seniority + factor(imputed_gender) +
                          factor(state) + factor(chamber) + factor(party) + factor(election_year),
                        estimand = "ATT",
                        method = "cem",
                        data = trimmed_data)
matches_maj_25 <- matchit(viable_25 ~ party_pres_vs + seniority + factor(imputed_gender) +
                         factor(state) + factor(chamber) + factor(party) + factor(election_year),
                       estimand = "ATT",
                       method = "cem",
                       data = maj_data)
matches_min_25 <- matchit(viable_25 ~ party_pres_vs + seniority + factor(imputed_gender) +
                         factor(state) + factor(chamber) + factor(party) + factor(election_year),
                       estimand = "ATT",
                       method = "cem",
                       data = min_data)
full.fit.25 <- felm(avg_opp_prop ~ viable_25 + party_pres_vs + seniority + factor(imputed_gender) |
                   interaction(state, chamber, party, election_year) | 0 | klarner_id,
                 data = trimmed_data, weights = matches_full_25$weights)
maj.fit.25 <- felm(avg_opp_prop ~ viable_25 + party_pres_vs + seniority + factor(imputed_gender) |
                  interaction(state, chamber, party, election_year) | 0 | klarner_id,
                data = maj_data, weights = matches_maj_25$weights)
min.fit.25 <- felm(avg_opp_prop ~ viable_25 + party_pres_vs + seniority + factor(imputed_gender) |
                  interaction(state, chamber, party, election_year) | 0 | klarner_id,
                data = min_data, weights = matches_min_25$weights)
full.np.fit.25 <- felm(extremity ~ viable_25 + party_pres_vs + seniority + factor(imputed_gender) |
                      interaction(state, chamber, party, election_year) | 0 | klarner_id,
                    data = trimmed_data, weights = matches_full_25$weights)
maj.np.fit.25 <- felm(extremity ~ viable_25 + party_pres_vs + seniority + factor(imputed_gender) |
                     interaction(state, chamber, party, election_year) | 0 | klarner_id,
                   data = maj_data, weights = matches_maj_25$weights)
min.np.fit.25 <- felm(extremity ~ viable_25 + party_pres_vs + seniority + factor(imputed_gender) |
                     interaction(state, chamber, party, election_year) | 0 | klarner_id,
                   data = min_data, weights = matches_min_25$weights)
matches_full_33 <- matchit(viable_33 ~ party_pres_vs + seniority + factor(imputed_gender) +
                             factor(state) + factor(chamber) + factor(party) + factor(election_year),
                           estimand = "ATT",
                           method = "cem",
                           data = trimmed_data)
matches_maj_33 <- matchit(viable_33 ~ party_pres_vs + seniority + factor(imputed_gender) +
                            factor(state) + factor(chamber) + factor(party) + factor(election_year),
                          estimand = "ATT",
                          method = "cem",
                          data = maj_data)
matches_min_33 <- matchit(viable_33 ~ party_pres_vs + seniority + factor(imputed_gender) +
                            factor(state) + factor(chamber) + factor(party) + factor(election_year),
                          estimand = "ATT",
                          method = "cem",
                          data = min_data)
full.fit.33 <- felm(avg_opp_prop ~ viable_33 + party_pres_vs + seniority + factor(imputed_gender) |
                      interaction(state, chamber, party, election_year) | 0 | klarner_id,
                    data = trimmed_data, weights = matches_full_33$weights)
maj.fit.33 <- felm(avg_opp_prop ~ viable_33 + party_pres_vs + seniority + factor(imputed_gender) |
                     interaction(state, chamber, party, election_year) | 0 | klarner_id,
                   data = maj_data, weights = matches_maj_33$weights)
min.fit.33 <- felm(avg_opp_prop ~ viable_33 + party_pres_vs + seniority + factor(imputed_gender) |
                     interaction(state, chamber, party, election_year) | 0 | klarner_id,
                   data = min_data, weights = matches_min_33$weights)
full.np.fit.33 <- felm(extremity ~ viable_33 + party_pres_vs + seniority + factor(imputed_gender) |
                         interaction(state, chamber, party, election_year) | 0 | klarner_id,
                       data = trimmed_data, weights = matches_full_33$weights)
maj.np.fit.33 <- felm(extremity ~ viable_33 + party_pres_vs + seniority + factor(imputed_gender) |
                        interaction(state, chamber, party, election_year) | 0 | klarner_id,
                      data = maj_data, weights = matches_maj_33$weights)
min.np.fit.33 <- felm(extremity ~ viable_33 + party_pres_vs + seniority + factor(imputed_gender) |
                        interaction(state, chamber, party, election_year) | 0 | klarner_id,
                      data = min_data, weights = matches_min_33$weights)
matches_full_50 <- matchit(viable_50 ~ party_pres_vs + seniority + factor(imputed_gender) +
                             factor(state) + factor(chamber) + factor(party) + factor(election_year),
                           estimand = "ATT",
                           method = "cem",
                           data = trimmed_data)
matches_maj_50 <- matchit(viable_50 ~ party_pres_vs + seniority + factor(imputed_gender) +
                            factor(state) + factor(chamber) + factor(party) + factor(election_year),
                          estimand = "ATT",
                          method = "cem",
                          data = maj_data)
matches_min_50 <- matchit(viable_50 ~ party_pres_vs + seniority + factor(imputed_gender) +
                            factor(state) + factor(chamber) + factor(party) + factor(election_year),
                          estimand = "ATT",
                          method = "cem",
                          data = min_data)
full.fit.50 <- felm(avg_opp_prop ~ viable_50 + party_pres_vs + seniority + factor(imputed_gender) |
                      interaction(state, chamber, party, election_year) | 0 | klarner_id,
                    data = trimmed_data, weights = matches_full_50$weights)
maj.fit.50 <- felm(avg_opp_prop ~ viable_50 + party_pres_vs + seniority + factor(imputed_gender) |
                     interaction(state, chamber, party, election_year) | 0 | klarner_id,
                   data = maj_data, weights = matches_maj_50$weights)
min.fit.50 <- felm(avg_opp_prop ~ viable_50 + party_pres_vs + seniority + factor(imputed_gender) |
                     interaction(state, chamber, party, election_year) | 0 | klarner_id,
                   data = min_data, weights = matches_min_50$weights)
full.np.fit.50 <- felm(extremity ~ viable_50 + party_pres_vs + seniority + factor(imputed_gender) |
                         interaction(state, chamber, party, election_year) | 0 | klarner_id,
                       data = trimmed_data, weights = matches_full_50$weights)
maj.np.fit.50 <- felm(extremity ~ viable_50 + party_pres_vs + seniority + factor(imputed_gender) |
                        interaction(state, chamber, party, election_year) | 0 | klarner_id,
                      data = maj_data, weights = matches_maj_50$weights)
min.np.fit.50 <- felm(extremity ~ viable_50 + party_pres_vs + seniority + factor(imputed_gender) |
                        interaction(state, chamber, party, election_year) | 0 | klarner_id,
                      data = min_data, weights = matches_min_50$weights)

### Table F.1 ------------------------------------------------------------------
# Table F.1, first row
summary(full.fit)$coefficients[1,c(1,2,4)]
summary(maj.fit)$coefficients[1,c(1,2,4)]
summary(min.fit)$coefficients[1,c(1,2,4)]
summary(full.np.fit)$coefficients[1,c(1,2,4)]
summary(maj.np.fit)$coefficients[1,c(1,2,4)]
summary(min.np.fit)$coefficients[1,c(1,2,4)]
# Table F.1, second row
summary(full.fit.25)$coefficients[1,c(1,2,4)]
summary(maj.fit.25)$coefficients[1,c(1,2,4)]
summary(min.fit.25)$coefficients[1,c(1,2,4)]
summary(full.np.fit.25)$coefficients[1,c(1,2,4)]
summary(maj.np.fit.25)$coefficients[1,c(1,2,4)]
summary(min.np.fit.25)$coefficients[1,c(1,2,4)]
# Table F.1, third row
summary(full.fit.33)$coefficients[1,c(1,2,4)]
summary(maj.fit.33)$coefficients[1,c(1,2,4)]
summary(min.fit.33)$coefficients[1,c(1,2,4)]
summary(full.np.fit.33)$coefficients[1,c(1,2,4)]
summary(maj.np.fit.33)$coefficients[1,c(1,2,4)]
summary(min.np.fit.33)$coefficients[1,c(1,2,4)]
# Table F.1, fourth row
summary(full.fit.50)$coefficients[1,c(1,2,4)]
summary(maj.fit.50)$coefficients[1,c(1,2,4)]
summary(min.fit.50)$coefficients[1,c(1,2,4)]
summary(full.np.fit.50)$coefficients[1,c(1,2,4)]
summary(maj.np.fit.50)$coefficients[1,c(1,2,4)]
summary(min.np.fit.50)$coefficients[1,c(1,2,4)]

# Run analyses where viability incorporates viability for US Senate
matches_full_sen <- matchit(viable_sen ~ party_pres_vs + seniority + factor(imputed_gender) +
                              factor(state) + factor(chamber) + factor(party) + factor(election_year),
                            estimand = "ATT",
                            method = "cem",
                            data = trimmed_data)
matches_maj_sen <- matchit(viable_sen ~ party_pres_vs + seniority + factor(imputed_gender) +
                             factor(state) + factor(chamber) + factor(party) + factor(election_year),
                           estimand = "ATT",
                           method = "cem",
                           data = maj_data)
matches_min_sen <- matchit(viable_sen ~ party_pres_vs + seniority + factor(imputed_gender) +
                             factor(state) + factor(chamber) + factor(party) + factor(election_year),
                           estimand = "ATT",
                           method = "cem",
                           data = min_data)
full.fit.sen <- felm(avg_opp_prop ~ viable_sen + party_pres_vs + seniority + factor(imputed_gender) |
                       interaction(state, chamber, party, election_year) | 0 | klarner_id,
                     data = trimmed_data, weights = matches_full_sen$weights)
maj.fit.sen <- felm(avg_opp_prop ~ viable_sen + party_pres_vs + seniority + factor(imputed_gender) |
                      interaction(state, chamber, party, election_year) | 0 | klarner_id,
                    data = maj_data, weights = matches_maj_sen$weights)
min.fit.sen <- felm(avg_opp_prop ~ viable_sen + party_pres_vs + seniority + factor(imputed_gender) |
                      interaction(state, chamber, party, election_year) | 0 | klarner_id,
                    data = min_data, weights = matches_min_sen$weights)
full.np.fit.sen <- felm(extremity ~ viable_sen + party_pres_vs + seniority + factor(imputed_gender) |
                          interaction(state, chamber, party, election_year) | 0 | klarner_id,
                        data = trimmed_data, weights = matches_full_sen$weights)
maj.np.fit.sen <- felm(extremity ~ viable_sen + party_pres_vs + seniority + factor(imputed_gender) |
                         interaction(state, chamber, party, election_year) | 0 | klarner_id,
                       data = maj_data, weights = matches_maj_sen$weights)
min.np.fit.sen <- felm(extremity ~ viable_sen + party_pres_vs + seniority + factor(imputed_gender) |
                         interaction(state, chamber, party, election_year) | 0 | klarner_id,
                       data = min_data, weights = matches_min_sen$weights)

### Table F.2 ------------------------------------------------------------------
stargazer(full.fit.sen, maj.fit.sen, min.fit.sen)
stargazer(full.np.fit.sen, maj.np.fit.sen, min.np.fit.sen)

# Run analyses with different overlap thresholds that incorporate viability for senate
# Run matches for analyses with different overlap thresholds
matches_full_25_sen <- matchit(viable_25_sen ~ party_pres_vs + seniority + factor(imputed_gender) +
                                 factor(state) + factor(chamber) + factor(party) + factor(election_year),
                               estimand = "ATT",
                               method = "cem",
                               data = trimmed_data)
matches_maj_25_sen <- matchit(viable_25_sen ~ party_pres_vs + seniority + factor(imputed_gender) +
                                factor(state) + factor(chamber) + factor(party) + factor(election_year),
                              estimand = "ATT",
                              method = "cem",
                              data = maj_data)
matches_min_25_sen <- matchit(viable_25_sen ~ party_pres_vs + seniority + factor(imputed_gender) +
                                factor(state) + factor(chamber) + factor(party) + factor(election_year),
                              estimand = "ATT",
                              method = "cem",
                              data = min_data)
full.fit.25.sen <- felm(avg_opp_prop ~ viable_25_sen + party_pres_vs + seniority + factor(imputed_gender) |
                          interaction(state, chamber, party, election_year) | 0 | klarner_id,
                        data = trimmed_data, weights = matches_full_25_sen$weights)
maj.fit.25.sen <- felm(avg_opp_prop ~ viable_25_sen + party_pres_vs + seniority + factor(imputed_gender) |
                         interaction(state, chamber, party, election_year) | 0 | klarner_id,
                       data = maj_data, weights = matches_maj_25_sen$weights)
min.fit.25.sen <- felm(avg_opp_prop ~ viable_25_sen + party_pres_vs + seniority + factor(imputed_gender) |
                         interaction(state, chamber, party, election_year) | 0 | klarner_id,
                       data = min_data, weights = matches_min_25_sen$weights)
full.np.fit.25.sen <- felm(extremity ~ viable_25_sen + party_pres_vs + seniority + factor(imputed_gender) |
                             interaction(state, chamber, party, election_year) | 0 | klarner_id,
                           data = trimmed_data, weights = matches_full_25_sen$weights)
maj.np.fit.25.sen <- felm(extremity ~ viable_25_sen + party_pres_vs + seniority + factor(imputed_gender) |
                            interaction(state, chamber, party, election_year) | 0 | klarner_id,
                          data = maj_data, weights = matches_maj_25_sen$weights)
min.np.fit.25.sen <- felm(extremity ~ viable_25_sen + party_pres_vs + seniority + factor(imputed_gender) |
                            interaction(state, chamber, party, election_year) | 0 | klarner_id,
                          data = min_data, weights = matches_min_25_sen$weights)
matches_full_33_sen <- matchit(viable_33_sen ~ party_pres_vs + seniority + factor(imputed_gender) +
                                 factor(state) + factor(chamber) + factor(party) + factor(election_year),
                               estimand = "ATT",
                               method = "cem",
                               data = trimmed_data)
matches_maj_33_sen <- matchit(viable_33_sen ~ party_pres_vs + seniority + factor(imputed_gender) +
                                factor(state) + factor(chamber) + factor(party) + factor(election_year),
                              estimand = "ATT",
                              method = "cem",
                              data = maj_data)
matches_min_33_sen <- matchit(viable_33_sen ~ party_pres_vs + seniority + factor(imputed_gender) +
                                factor(state) + factor(chamber) + factor(party) + factor(election_year),
                              estimand = "ATT",
                              method = "cem",
                              data = min_data)
full.fit.33.sen <- felm(avg_opp_prop ~ viable_33_sen + party_pres_vs + seniority + factor(imputed_gender) |
                          interaction(state, chamber, party, election_year) | 0 | klarner_id,
                        data = trimmed_data, weights = matches_full_33_sen$weights)
maj.fit.33.sen <- felm(avg_opp_prop ~ viable_33_sen + party_pres_vs + seniority + factor(imputed_gender) |
                         interaction(state, chamber, party, election_year) | 0 | klarner_id,
                       data = maj_data, weights = matches_maj_33_sen$weights)
min.fit.33.sen <- felm(avg_opp_prop ~ viable_33_sen + party_pres_vs + seniority + factor(imputed_gender) |
                         interaction(state, chamber, party, election_year) | 0 | klarner_id,
                       data = min_data, weights = matches_min_33_sen$weights)
full.np.fit.33.sen <- felm(extremity ~ viable_33_sen + party_pres_vs + seniority + factor(imputed_gender) |
                             interaction(state, chamber, party, election_year) | 0 | klarner_id,
                           data = trimmed_data, weights = matches_full_33_sen$weights)
maj.np.fit.33.sen <- felm(extremity ~ viable_33_sen + party_pres_vs + seniority + factor(imputed_gender) |
                            interaction(state, chamber, party, election_year) | 0 | klarner_id,
                          data = maj_data, weights = matches_maj_33_sen$weights)
min.np.fit.33.sen <- felm(extremity ~ viable_33_sen + party_pres_vs + seniority + factor(imputed_gender) |
                            interaction(state, chamber, party, election_year) | 0 | klarner_id,
                          data = min_data, weights = matches_min_33_sen$weights)
matches_full_50_sen <- matchit(viable_50_sen ~ party_pres_vs + seniority + factor(imputed_gender) +
                                 factor(state) + factor(chamber) + factor(party) + factor(election_year),
                               estimand = "ATT",
                               method = "cem",
                               data = trimmed_data)
matches_maj_50_sen <- matchit(viable_50_sen ~ party_pres_vs + seniority + factor(imputed_gender) +
                                factor(state) + factor(chamber) + factor(party) + factor(election_year),
                              estimand = "ATT",
                              method = "cem",
                              data = maj_data)
matches_min_50_sen <- matchit(viable_50_sen ~ party_pres_vs + seniority + factor(imputed_gender) +
                                factor(state) + factor(chamber) + factor(party) + factor(election_year),
                              estimand = "ATT",
                              method = "cem",
                              data = min_data)
full.fit.50.sen <- felm(avg_opp_prop ~ viable_50_sen + party_pres_vs + seniority + factor(imputed_gender) |
                          interaction(state, chamber, party, election_year) | 0 | klarner_id,
                        data = trimmed_data, weights = matches_full_50_sen$weights)
maj.fit.50.sen <- felm(avg_opp_prop ~ viable_50_sen + party_pres_vs + seniority + factor(imputed_gender) |
                         interaction(state, chamber, party, election_year) | 0 | klarner_id,
                       data = maj_data, weights = matches_maj_50_sen$weights)
min.fit.50.sen <- felm(avg_opp_prop ~ viable_50_sen + party_pres_vs + seniority + factor(imputed_gender) |
                         interaction(state, chamber, party, election_year) | 0 | klarner_id,
                       data = min_data, weights = matches_min_50_sen$weights)
full.np.fit.50.sen <- felm(extremity ~ viable_50_sen + party_pres_vs + seniority + factor(imputed_gender) |
                             interaction(state, chamber, party, election_year) | 0 | klarner_id,
                           data = trimmed_data, weights = matches_full_50_sen$weights)
maj.np.fit.50.sen <- felm(extremity ~ viable_50_sen + party_pres_vs + seniority + factor(imputed_gender) |
                            interaction(state, chamber, party, election_year) | 0 | klarner_id,
                          data = maj_data, weights = matches_maj_50_sen$weights)
min.np.fit.50.sen <- felm(extremity ~ viable_50_sen + party_pres_vs + seniority + factor(imputed_gender) |
                            interaction(state, chamber, party, election_year) | 0 | klarner_id,
                          data = min_data, weights = matches_min_50_sen$weights)

### Table F.3 ------------------------------------------------------------------
# Table F.3, first row
summary(full.fit.sen)$coefficients[1,c(1,2,4)]
summary(maj.fit.sen)$coefficients[1,c(1,2,4)]
summary(min.fit.sen)$coefficients[1,c(1,2,4)]
summary(full.np.fit.sen)$coefficients[1,c(1,2,4)]
summary(maj.np.fit.sen)$coefficients[1,c(1,2,4)]
summary(min.np.fit.sen)$coefficients[1,c(1,2,4)]
# Table F.3, second row
summary(full.fit.25.sen)$coefficients[1,c(1,2,4)]
summary(maj.fit.25.sen)$coefficients[1,c(1,2,4)]
summary(min.fit.25.sen)$coefficients[1,c(1,2,4)]
summary(full.np.fit.25.sen)$coefficients[1,c(1,2,4)]
summary(maj.np.fit.25.sen)$coefficients[1,c(1,2,4)]
summary(min.np.fit.25.sen)$coefficients[1,c(1,2,4)]
# Table F.3, third row
summary(full.fit.33.sen)$coefficients[1,c(1,2,4)]
summary(maj.fit.33.sen)$coefficients[1,c(1,2,4)]
summary(min.fit.33.sen)$coefficients[1,c(1,2,4)]
summary(full.np.fit.33.sen)$coefficients[1,c(1,2,4)]
summary(maj.np.fit.33.sen)$coefficients[1,c(1,2,4)]
summary(min.np.fit.33.sen)$coefficients[1,c(1,2,4)]
# Table F.3, fourth row
summary(full.fit.50.sen)$coefficients[1,c(1,2,4)]
summary(min.fit.50.sen)$coefficients[1,c(1,2,4)]
summary(maj.fit.50.sen)$coefficients[1,c(1,2,4)]
summary(full.np.fit.50.sen)$coefficients[1,c(1,2,4)]
summary(maj.np.fit.50.sen)$coefficients[1,c(1,2,4)]
summary(min.np.fit.50.sen)$coefficients[1,c(1,2,4)]


# Build data for added leadership covariates
expanded_match_data <-  full_data[complete.cases(full_data %>% select(-prior_office)),]
expanded_match_maj_data <- expanded_match_data %>% filter(majority_member == 1)
expanded_match_min_data <- expanded_match_data %>% filter(majority_member == 0)

# Build matches using just observations that have added leadership covariates available
expanded_matches_full <- matchit(viable ~ party_pres_vs + seniority + factor(imputed_gender) +
                          factor(state) + factor(chamber) + factor(party) + factor(election_year),
                        estimand = "ATT",
                        method = "cem",
                        data = expanded_match_data)

expanded_matches_maj <- matchit(viable ~ party_pres_vs + seniority + factor(imputed_gender) +
                         factor(state) + factor(chamber) + factor(party) + factor(election_year),
                       estimand = "ATT",
                       method = "cem",
                       data = expanded_match_maj_data)

expanded_matches_min <- matchit(viable ~ party_pres_vs + seniority + factor(imputed_gender) +
                         factor(state) + factor(chamber) + factor(party) + factor(election_year),
                       estimand = "ATT",
                       method = "cem",
                       data = expanded_match_min_data)

# Estimate treatment effects with those matches
expanded.match.full.fit <- felm(avg_opp_prop ~ viable + party_pres_vs + seniority + factor(imputed_gender) |
                   interaction(state, chamber, party, election_year) | 0 | klarner_id,
                 data = expanded_match_data, weights = expanded_matches_full$weights)
expanded.match.maj.fit <- felm(avg_opp_prop ~ viable + party_pres_vs + seniority + factor(imputed_gender) |
                  interaction(state, chamber, party, election_year) | 0 | klarner_id,
                data = expanded_match_maj_data, weights = expanded_matches_maj$weights)
expanded.match.min.fit <- felm(avg_opp_prop ~ viable + party_pres_vs + seniority + factor(imputed_gender) |
                  interaction(state, chamber, party, election_year) | 0 | klarner_id,
                data = expanded_match_min_data, weights = expanded_matches_min$weights)
expanded.match.full.np.fit <- felm(extremity ~ viable + party_pres_vs + seniority + factor(imputed_gender) |
                      interaction(state, chamber, party, election_year) | 0 | klarner_id,
                    data = expanded_match_data, weights = expanded_matches_full$weights)
expanded.match.maj.np.fit <- felm(extremity ~ viable + party_pres_vs + seniority + factor(imputed_gender) |
                     interaction(state, chamber, party, election_year) | 0 | klarner_id,
                   data = expanded_match_maj_data, weights = expanded_matches_maj$weights)
expanded.match.min.np.fit <- felm(extremity ~ viable + party_pres_vs + seniority + factor(imputed_gender) |
                     interaction(state, chamber, party, election_year) | 0 | klarner_id,
                   data = expanded_match_min_data, weights = expanded_matches_min$weights)

### Table G.1 ------------------------------------------------------------------
stargazer(expanded.match.full.fit, expanded.match.maj.fit, expanded.match.min.fit)
stargazer(expanded.match.full.np.fit, expanded.match.maj.np.fit, expanded.match.min.np.fit)
c(sum(expanded_matches_full$weights > 0), sum(expanded_matches_maj$weights > 0), sum(expanded_matches_min$weights > 0))

### Table G.2 ------------------------------------------------------------------
# Regressions for Table G.2
expanded.reg.full.fit <- felm(avg_opp_prop ~ viable + party_pres_vs + seniority + imputed_gender + comm_chair + leader |
                       interaction(state, chamber, party, election_year) | 0 | klarner_id,
                     data = full_data)
expanded.reg.maj.fit <- felm(avg_opp_prop ~ viable + party_pres_vs + seniority + imputed_gender + comm_chair + leader  |
                      interaction(state, chamber, party, election_year) | 0 | klarner_id,
                    data = maj_data)
expanded.reg.min.fit <- felm(avg_opp_prop ~ viable + party_pres_vs + seniority + imputed_gender + comm_chair + leader |
                      interaction(state, chamber, party, election_year) | 0 | klarner_id,
                    data = min_data)

expanded.reg.full.np.fit <- felm(extremity ~ viable + party_pres_vs + seniority + imputed_gender + comm_chair + leader |
                          interaction(state, chamber, party, election_year) | 0 | klarner_id,
                        data = full_data)
expanded.reg.maj.np.fit <- felm(extremity ~ viable + party_pres_vs + seniority + imputed_gender + comm_chair + leader  |
                         interaction(state, chamber, party, election_year) | 0 | klarner_id,
                       data = maj_data)
expanded.reg.min.np.fit <- felm(extremity ~ viable + party_pres_vs + seniority + imputed_gender + comm_chair + leader |
                         interaction(state, chamber, party, election_year) | 0 | klarner_id,
                       data = min_data)

# Table G.2
stargazer(expanded.reg.full.fit, expanded.reg.maj.fit, expanded.reg.min.fit)
stargazer(expanded.reg.full.np.fit, expanded.reg.maj.np.fit, expanded.reg.min.np.fit)

### Table G.3 ------------------------------------------------------------------
# Regressions for Table G.3
prior.reg.full.fit <- felm(avg_opp_prop ~ viable + party_pres_vs + seniority + imputed_gender + comm_chair + leader + prior_office |
                       interaction(state, chamber, party, election_year) | 0 | klarner_id,
                     data = full_data)
prior.reg.maj.fit <- felm(avg_opp_prop ~ viable + party_pres_vs + seniority + imputed_gender + comm_chair + leader + prior_office |
                      interaction(state, chamber, party, election_year) | 0 | klarner_id,
                    data = maj_data)
prior.reg.min.fit <- felm(avg_opp_prop ~ viable + party_pres_vs + seniority + imputed_gender + comm_chair + leader + prior_office |
                      interaction(state, chamber, party, election_year) | 0 | klarner_id,
                    data = min_data)

prior.reg.full.np.fit <- felm(extremity ~ viable + party_pres_vs + seniority + imputed_gender + comm_chair + leader + prior_office |
                          interaction(state, chamber, party, election_year) | 0 | klarner_id,
                        data = full_data)
prior.reg.maj.np.fit <- felm(extremity ~ viable + party_pres_vs + seniority + imputed_gender + comm_chair + leader + prior_office |
                         interaction(state, chamber, party, election_year) | 0 | klarner_id,
                       data = maj_data)
prior.reg.min.np.fit <- felm(extremity ~ viable + party_pres_vs + seniority + imputed_gender + comm_chair + leader + prior_office |
                         interaction(state, chamber, party, election_year) | 0 | klarner_id,
                       data = min_data)

# Table G.3
stargazer(prior.reg.full.fit, prior.reg.maj.fit, prior.reg.min.fit)
stargazer(prior.reg.full.np.fit, prior.reg.maj.np.fit, prior.reg.min.np.fit)

### Table H.1 ------------------------------------------------------------------
# Run analyses for Table H.1
ran.maj.fit <- felm(cong_ran ~ avg_opp_prop + party_pres_vs + seniority | interaction(eligible_district, chamber, election_year) | 0 | state,
                    data = ran_data, subset = which(majority_member == 1))
ran.min.fit <- felm(cong_ran ~ avg_opp_prop + party_pres_vs + seniority | interaction(eligible_district, chamber, election_year) | 0 | state,
                    data = ran_data, subset = which(majority_member == 0))
lag.ran.maj.fit <- felm(cong_ran ~ lag_avg_opp_prop + party_pres_vs + seniority | interaction(eligible_district, chamber, election_year) | 0 | state,
                        data = ran_data, subset = which(majority_member == 1))
lag.ran.min.fit <- felm(cong_ran ~ lag_avg_opp_prop + party_pres_vs + seniority | interaction(eligible_district, chamber, election_year) | 0 | state,
                        data = ran_data, subset = which(majority_member == 0))

# Table H.1
stargazer(ran.maj.fit, ran.min.fit, lag.ran.maj.fit, lag.ran.min.fit)

